package LeetCode.month01.day14;

import java.util.ArrayList;
import java.util.Arrays;

public class No31 {
    public static void main(String[] args) {
        int[] nums = new int[]{2,1,3};
        nextPermutation(nums);
        System.out.println(Arrays.toString(nums));
    }
    public static void nextPermutation(int[] nums) {
        for (int i = nums.length-2; i >=0; i--) {
            if(nums[i]<nums[i+1]){
                int left = i+1,right = nums.length-1;
                for (int j = nums.length - 1; j >= 0; j--) {
                    if(nums[j]>nums[i]){
                        int temp = nums[j];
                        nums[j]=nums[i];
                        nums[i] = temp;
                        break;
                    }
                }
                while(left<right){
                    int temp = nums[left];
                    nums[left]= nums[right];
                    nums[right]=temp;
                    left++;
                    right--;
                }
                break;
            }
            if(i==0){
                int left = 0,right=nums.length-1;
                while(left<right){
                    int temp = nums[left];
                    nums[left]= nums[right];
                    nums[right]=temp;
                    left++;
                    right--;
                }
            }
        }
    }
}
